package com.hdjk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.entity.HoldingFundEntity;
import com.hdjk.view.util.StringAppUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sxy on 2017/11/3.
 */

public class RedeemConfigAdapter extends BaseAdapter {
    private List<HoldingFundEntity> mList;
    private Context mContext;
    private String mTotal;

    public RedeemConfigAdapter(Context context, String tatal) {
        mContext = context;
        mTotal = tatal;
    }

    public void setDataList(List<HoldingFundEntity> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public HoldingFundEntity getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_redeem_confirm, null);
            holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_availableunit = (TextView) convertView.findViewById(R.id.tv_availableunit);
            holder.tv_percent = (TextView) convertView.findViewById(R.id.tv_percent);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        HoldingFundEntity entity = mList.get(position);
        holder.tv_id.setText(entity.getFundCode());
        holder.tv_availableunit.setText("赎回份额：" + entity.getAvailableUnit() + "份");
        holder.tv_name.setText(entity.getFundName());

        BigDecimal bdMoney=new BigDecimal(entity.getAvailableUnit()).multiply(new BigDecimal(entity.getNav())).setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bdCurrentTotalValue = new BigDecimal(mTotal);
        if (bdCurrentTotalValue.compareTo(BigDecimal.ZERO) == 0) {
            holder.tv_percent.setText("0.00%");
        } else {
            holder.tv_percent.setText(StringAppUtil.formatDecimal(bdMoney.multiply(new BigDecimal(100)).divide(bdCurrentTotalValue, 3).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(), 2) + "%");
        }
        return convertView;
    }

    class Holder {
        TextView tv_name;
        TextView tv_availableunit;
        TextView tv_id;
        TextView tv_percent;
    }
}
